package test.com.newproject.framework.mvp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

import test.com.newproject.framework.ui.BaseFragment;

/**
 * Created by zjh on 2017/8/14.
 */

public abstract class BaseMvpFragment extends BaseFragment implements IBaseView {
    protected List<BasePresenter> presenterList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        configPresenter();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void createPresenter(Class<? extends BasePresenter>... classes) {
        if (ObjectUtils.isEmpty(presenterList)) {
            presenterList = new ArrayList<>();
        }

        for (Class aClass : classes) {
            try {
                BasePresenter basePresenter = (BasePresenter) aClass.newInstance();
                basePresenter.setiView(this);
                presenterList.add(basePresenter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public <P extends BasePresenter> P getPresenter(Class<P> aClass) {
        for (BasePresenter basePresenter : presenterList) {
            if (basePresenter.getClass() == aClass) {
                return (P) basePresenter;
            }
        }

        return null;
    }
}
